package com.qianfeng.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qianfeng.biz.IUserBiz;
import com.qianfeng.biz.impl.UserBizImpl;
import com.qianfeng.commons.WebUtils;
import com.qianfeng.dto.PageBean;
import com.qianfeng.entity.User;
import com.qianfeng.util.CodeUtil;

public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private IUserBiz userBiz = new UserBizImpl();
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		PrintWriter out = response.getWriter();
		if("checkNameExits".equals(action)){
			checkN(request, out);
		}else if("register".equals(action)){
			register(request, out);
		}else if("modifeRegistInfo".equals(action)){
			modify(request, out);
		}else if("login".equals(action)){
			login(request, out);
		}else if("zhuxiao".equals(action)){
			zhuxiao(request, response);
		}else if("backlogin".equals(action)){
			bocklogin(request, out);
		}else if("queryAllUserInfo".equals(action)){
			queryfenye(request, response);
		}else if("toUpdate".equals(action)){
			toupdate(request, response);
		}else if("update".equals(action)){
			update(request, out);
		}else if("updateUserLockState".equals(action)){
			updatestatus(request, out);
		}else if("delete".equals(action)){
			delete(request, out);
		}else if("add".equals(action)){
			add(request, out);
		}else if("backExit".equals(action)){
			//退出账户
			request.getSession().removeAttribute("BACK_LOGIN_USER");
			response.sendRedirect("backLogin.jsp");
		}
		
	}

	private void add(HttpServletRequest request, PrintWriter out) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String username = request.getParameter("username");
		int password =WebUtils.StringToInt(request.getParameter("password"),-1);
		String nickname = request.getParameter("nickname");
		String role = request.getParameter("role");
		String sex = request.getParameter("sex");
		Date shengri = null;
		try {
			String birthday = request.getParameter("birthday");
			shengri = sdf.parse(birthday);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String code = request.getParameter("code");
		
		User user = new User(username, nickname, password, sex, shengri, phone, code, email, address, role, new Date());
		int result = userBiz.addUserInfo(user);
		if (result > 0) {
			out.write("<script>alert('添加用户成功');location.href='UserServlet?action=queryAllUserInfo&currentPage=1'</script>");
		} else {
			out.write("<script>alert('添加用户失败');location.href='UserServlet?action=queryAllUserInfo&currentPage=1'</script>");
		}
	}

	private void delete(HttpServletRequest request, PrintWriter out) {
		int id = WebUtils.StringToInt(request.getParameter("userid"), -1);
		
		int result = userBiz.deleteById(id);
		if(result>0){
			out.write("<script>alert('删除类别信息成功');location.href='UserServlet?action=queryAllUserInfo&currentPage=1'</script>");
		}else{
			out.write("<script>alert('删除类别信息失败');location.href='UserServlet?action=queryAllUserInfo&currentPage=1'</script>");
		}
	}

	private void updatestatus(HttpServletRequest request, PrintWriter out) {
		int userid = WebUtils.StringToInt(request.getParameter("userid"), -1);
		int lockstate = WebUtils.StringToInt(request.getParameter("lockstate"), -1);

		int result = userBiz.updateUserLockState(userid, lockstate);
		if (result > 0) {
			out.write("<script>alert('修改状态成功');location.href='back/UserServlet?action=queryAllUserInfo&currentPage=1'</script>");
		} else {
			out.write("<script>alert('修改状态失败');location.href='back/UserServlet?action=queryAllUserInfo&currentPage=1'</script>");
		}
	}

	private void update(HttpServletRequest request, PrintWriter out) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		int userid = WebUtils.StringToInt(request.getParameter("id"), -1);
		String username = request.getParameter("username");
		int password =WebUtils.StringToInt(request.getParameter("password"),-1);
		String nickname = request.getParameter("nickname");
		String role = request.getParameter("role");
		String sex = request.getParameter("sex");
		Date shengri = null;
		try {
			String birthday = request.getParameter("birthday");
			shengri = sdf.parse(birthday);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String code = request.getParameter("code");
		
		User user = new User(userid, username, nickname, password, sex, shengri, phone, code, email, address, role);
		int result = userBiz.updateUserInfo(user);
		if (result > 0) {
			out.write("<script>alert('修改用户成功');location.href='UserServlet?action=queryAllUserInfo&currentPage=1'</script>");
		} else {
			out.write("<script>alert('修改用户失败');location.href='UserServlet?action=queryAllUserInfo&currentPage=1'</script>");
		}
	}

	private void toupdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int userid = WebUtils.StringToInt(request.getParameter("userid"), -1);
		
		User user = userBiz.getUserById(userid);
		request.getSession().setAttribute("user", user);
		response.sendRedirect("user/updateuser.jsp");
	}

	private void queryfenye(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String cp = request.getParameter("currentPage");
		int currPage = Integer.parseInt(cp);
		PageBean page=userBiz.queryall(currPage);
		request.getSession().setAttribute("page", page);
		
		response.sendRedirect("user/userinfo.jsp");
	}

	private void bocklogin(HttpServletRequest request, PrintWriter out) {
		String username = request.getParameter("username");
		int password = WebUtils.StringToInt(request.getParameter("password"), -1);
		String code = request.getParameter("code");
		boolean code2 = CodeUtil.Code(request, code);	
		if(!code2){
			out.write("<script>alert('验证码错误，请重新输入');location.href='backLogin.jsp'</script>");
		}
		User user = userBiz.checkBackLogin(username, password);
		if (user != null) {
			out.write("<script>alert('登录成功');location.href='./back/main.jsp'</script>");
			request.getSession().setAttribute("BACK_LOGIN_USER", user);
		} else {
			out.write("<script>alert('登录失败');location.href='backLogin.jsp'</script>");
		}
	}

	private void login(HttpServletRequest request, PrintWriter out) {
		//登录
		String username = request.getParameter("username");
		int password = WebUtils.StringToInt(request.getParameter("password"), -1);
		String code = request.getParameter("code");
		boolean code2 = CodeUtil.Code(request, code);	
		if(!code2){
			out.write("<script>alert('验证码错误，请重新输入');location.href='login.jsp'</script>");
		}
		User user = new User(username, password);
		user = userBiz.checkLogin(user);
		if(user!=null){
			out.write("<script>alert('登录成功');location.href='home.jsp'</script>");
			request.getSession().setAttribute("LOGIN_USER", user);
		}else{
			out.write("<script>alert('登录失败');location.href='login.jsp'</script>");
		}
	}

	private void zhuxiao(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//退出账户
		request.getSession().removeAttribute("LOGIN_USER");
		response.sendRedirect("home.jsp");
	}

	private void modify(HttpServletRequest request, PrintWriter out) {
		String nick_name = request.getParameter("nick_name");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		String code = request.getParameter("code");
		
		//得到注册用户对象
		User user = (User) request.getSession().getAttribute("REGIST_USER");
		
		//组装一个对象，对象中有要修改的信息
		user = userBiz.modifyInfo(user,nick_name,sex,birthday,phone,email,address,code);
		//修改
		int result = userBiz.update(user);
		if(result>0){
			out.write("<script>alert('完善注册用户信息成功');location.href='home.jsp'</script>");
		}else{
			out.write("<script>alert('完善注册用户信息失败');location.href='home.jsp'</script>");
		}
	}

	private void register(HttpServletRequest request, PrintWriter out) {
		//注册
		String username = request.getParameter("username");
		String password1 = request.getParameter("password");
		System.out.println();
		int password =WebUtils.StringToInt(password1,-1);
		
		User user = new User(username,password);
		int result = userBiz.addUser(user);
		if(result>0){
			out.write("<script>alert('注册成功');location.href='information.jsp'</script>");
			request.getSession().setAttribute("REGIST_USER", user);
		}else{
			out.write("<script>alert('注册失败');location.href='regist.jsp'</script>");
		}
	}

	private void checkN(HttpServletRequest request, PrintWriter out) {
		String username = request.getParameter("username");
		int result = userBiz.checkNameExits(username);
		if(result>0){
			out.write("1");
		}else{
			out.write("0");
		}
	}

}
